package com.parking.mgr.common.exception.enums;


import com.parking.mgr.common.annotion.ExpEnumType;
import com.parking.mgr.common.constant.ExpEnumConstant;
import com.parking.mgr.common.exception.AuthException;
import com.parking.mgr.common.exception.BaseException;
import com.parking.mgr.common.exception.asserts.Assertion;
import com.parking.mgr.common.factory.ExpEnumCodeFactory;

/**
 * @ClassName AuthExceptionEnum
 * @Description 认证相关的异常的枚举
 *              认证和鉴权的区别：
 *              认证可以证明你能登录系统，认证的过程是校验token的过程
 *              鉴权可以证明你有系统的哪些权限，鉴权的过程是校验角色是否包含某些接口的权限
 * @Auther: zhijianpu
 * @Date: 2023/5/19
 */
@ExpEnumType(module = ExpEnumConstant.COMMON_EXCEPTION_ENUM, kind = ExpEnumConstant.COMMON_EXCEPTION_ENUM)
public enum AuthExceptionEnum implements IBaseExceptionEnum, Assertion {

    /**
     * 账号或密码为空
     */
    ACCOUNT_PWD_EMPTY(1, "账号或密码为空"),
    I18N_MESSAGE_ACCOUNT_PASSWORD_EMPTY(1, "i18n_message_account_password_empty"),

    /**
     * 账号密码错误
     */
    ACCOUNT_PWD_ERROR(2, "账号或密码错误"),
    I18N_MESSAGE_ACCOUNT_PASSWORD_ERROR(2, "i18n_message_account_password_error"),

    /**
     * 验证码错误
     */
    VALID_CODE_ERROR(3, "验证码错误"),
    I18N_MESSAGE_CAPTCHA_ERROR(3, "i18n_message_captcha_error"),

    /**
     * 请求token为空
     */
    REQUEST_TOKEN_EMPTY(4, "请求token为空，请携带token访问本接口"),

    /**
     * token格式不正确，token请以Bearer开头
     */
    NOT_VALID_TOKEN_TYPE(5, "token格式不正确，token请以Bearer开头，并且Bearer后边带一个空格"),

    /**
     * 请求token错误
     */
    REQUEST_TOKEN_ERROR(6, "请求token错误"),

    /**
     * 账号被冻结
     */
    ACCOUNT_FREEZE_ERROR(7, "账号被冻结，请联系管理员"),

    /**
     * 登录已过期
     */
    LOGIN_EXPIRED(8, "登录已过期，请重新登录"),
    I18N_MESSAGE_LOGIN_EXPIRED(8, "i18n_message_login_expired"),

    /**
     * 无登录用户
     */
    NO_LOGIN_USER(9, "无登录用户信息"),
    I18N_MESSAGE_NO_LOGIN_USER(9, "I18N_MESSAGE_NO_LOGIN_USER"),

    /**
     * 验证码错误
     */
    CONSTANT_EMPTY_ERROR(10, "验证码错误"),

    /**
     * 获取第三方用户信息异常
     */
    THIRD_EXCHANGE_ERROR(11, "获取第三方用户信息异常"),

    CLIENT_ID_OR_TOKEN_EMPTY(13, "客户端ID或LoginToken为空，请检查"),
    I18N_MESSAGE_CLIENT_ID_OR_LOGIN_TOKEN_EMPTY(13, "i18n_message_client_id_or_login_token_empty"),

    PWD_REGEX_ERROR(14, "密码长度在8到20位之间，必须包含至少一个大写字母、一个小写字母和一个数字"),
    I18N_MESSAGE_PASSWORD_REGEX_ERROR(14, "i18n_message_password_regex_error"),

    PWD_NOT_EXISTS(15, "未找到用户所属密码，请添加用户密码"),

    NOT_USER_INFO(16, "无用户信息"),
    I18N_MESSAGE_NOT_USER_INFO(16, "i18n_message_not_user_info"),

    NO_PERMISSION_URL(17, "无权限访问"),
    I18N_MESSAGE_NO_PERMISSION_URL(17, "i18n_message_no_permission_url"),

    /**
     * 验证码错误
     */
    VALID_CODE_INVALID(18, "验证码失效，请刷新验证码"),
    I18N_MESSAGE_VALID_CODE_INVALID(18, "i18n_message_valid_code_invalid"),


    /**
     * 绑定第三方登录用户
     */
    THIRD_USER_BIND(12, "");

    private final Integer code;

    private final String message;

    AuthExceptionEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    @Override
    public Integer getCode() {
        return ExpEnumCodeFactory.getExpEnumCode(this.getClass(), code);
    }

    @Override
    public String getMessage() {
        return message;
    }

    @Override
    public BaseException onAssertFailed(String errorMessage) {
        if (errorMessage == null || errorMessage.isEmpty()) {
            return new AuthException(this);
        } else {
            return new AuthException(this.getCode(), errorMessage);
        }
    }
}
